package com.prism.moodme.app;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import com.prism.moodme.data.Data;
import com.prism.moodme.util.Utility;

public class SearchActionListener implements ActionListener {

	private String query;
	private List<Data> _results;

	private MainFrame mainFrame;

	public SearchActionListener(MainFrame mainFrame){

		setFrame(mainFrame);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		setQuery((((JButton)e.getSource()).getAction().toString()));
		setQuery("2011\t"+getQuery());
		setResults(Utility.getData(getQuery()));

		if(getResults() != null){
			repaintMap();
			repaintDayChart();
			repaintHourChart();
			getFrame().repaint();
		}else{
			getPopup();
			repaintAll();
		}

	}

	private void repaintAll() {
		getFrame().getResultPane().getDayPanel().setChart();
		getFrame().getResultPane().getHourPanel().setChart();
		getFrame().getResultPane().getMapPanel().setMap();
	}
	private void repaintDayChart() {
		String dayChart = Utility.getDayChart(getResults());
//		System.out.println(dayChart);
		getFrame().getResultPane().getDayPanel().setChart(dayChart);
	}
	private void repaintHourChart() {
		String hourChart = Utility.getHourChart(getResults());
//		System.out.println(hourChart);
		getFrame().getResultPane().getHourPanel().setChart(hourChart);
	}
	private void repaintMap() {
		String map = Utility.getMap(getResults());
		System.out.println(map);
		getFrame().getResultPane().getMapPanel().setMap(map);
	}
	public void setFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}
	public MainFrame getFrame() {
		return this.mainFrame;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getQuery() {
		return query;
	}
	public void setResults(List<Data> _results) {
		this._results = _results;
	}
	public List<Data> getResults() {
		return _results;
	}

	public void getPopup(){
		JOptionPane.showMessageDialog(getFrame(), "No events matching this search criteria!");
	}

}
